public class Peopletest40 {
    public static void main(String[] args) {
        //1.创建一个数组来储存学生对象
        People[] arr = new People[3];
        //2.创建学生对象并添加到数组中
        People stu1 = new People(1, "zhang", 21);
        People stu2 = new People(2, "wang", 23);
        People stu3 = new People(3, "zhao", 22);
        //3.把学生添加到数组中
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        //要求1:再次添加一个学生对象,并在添加的时候进行学号唯一性的判断
        //要求2:添加完毕后,遍历所有的学生信息
        People stu4 = new People(3, "li", 22);
        //唯一性判断
        //已存在--不需要添加
        //不存在--可以添加到数组中
        boolean flag = contain(arr, stu4.getId());
        if (flag) {
            //已存在--不需要添加
            System.out.println("该学号已存在,请重新尝试");
        } else {
            //不存在--可以添加到数组中
            int count = getCount(arr);
            if (count ==arr.length){
                //如果数组已满--需要重新创建数组来添加
                People[] newarr = creatNewarr(arr);
                newarr[count] = stu4;
                printArray(newarr);
            }else{
                //如果数组不满--直接添加
                arr[count] = stu4;
                printArray(arr);
            }
        }
    }
    public static void printArray(People[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                System.out.println(arr[i].getId() + "," + arr[i].getName() + "," + arr[i].getAge());
            }
        }
    }
    public static People[] creatNewarr(People[] arr) {
        People[] newarr = new People[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            //把老数组中的元素添加到新数组中
            newarr[i] = arr[i];
        }
        return newarr;
    }
    public static int getCount(People[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }
     //要做什么?  判断唯一性
     //需要什么?   数组,学号
    //调用处是否需要继续使用方法的结果?  需要
    public static boolean contain(People[] arr,int id) {
        for (int i = 0; i < arr.length; i++) {
            //依次获取到数组里的每一个对象
            People p = arr[i];
            //获取数组中学生的id
            int pid = p.getId();
            if (pid == id) {
                return true;
            }
        }
        return false;
    }
}

